package com.example.demo;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import java.lang.annotation.Repeatable;

public class MainActivity extends AppCompatActivity {
    private ImageView iv_wifi;
    private ImageView iv_fly;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_wifi = findViewById(R.id.iv_wifi);
        iv_wifi.setImageResource(R.drawable.wifi);
        AnimationDrawable animationDrawable = (AnimationDrawable) iv_wifi.getDrawable();
        animationDrawable.start();

        iv_fly = findViewById(R.id.iv_fly);

//        startAlphaAnimation();
//        startScaleAnimation();

//        startTranslateAnimation();
//        startRotateAnimation();
        startAnimationSet();
    }

    private void startAnimationSet() {
        AnimationSet animationSet = new AnimationSet(true);
        RotateAnimation rotateAnimation =new RotateAnimation(0, 360);
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300, 0, 300);
        ScaleAnimation scaleAnimation =new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);
        animationSet.setDuration(3000);
        animationSet.setRepeatCount(3);
        animationSet.setFillAfter(true);
        iv_fly.startAnimation(animationSet);

    }

    private void startRotateAnimation() {
        RotateAnimation rotateAnimation =new RotateAnimation(0, 360, 300, 400);
        rotateAnimation.setDuration(3000);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(3);
        iv_fly.startAnimation(rotateAnimation);
    }

    private void startTranslateAnimation() {
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 300, 0, 300);
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        iv_fly.startAnimation(translateAnimation);
    }

    private void startScaleAnimation() {
//        ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
//        iv_fly.startAnimation(scaleAnimation);
        ScaleAnimation scaleAnimation =new ScaleAnimation(2.0f, 0.0f, 2.0f, 0.0f, 0.5f, 0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatCount(3);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        iv_fly.startAnimation(scaleAnimation);
    }

    private void startAlphaAnimation() {
        // 通过xml方式加载动画
        AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(MainActivity.this,
                R.anim.alpha);
        iv_fly.startAnimation(alphaAnimation);

        // 通过代码实现透明度动画
//        AlphaAnimation alphaAnimation2 = new AlphaAnimation(1.0f, 0.0f);
//        alphaAnimation2.setRepeatCount(3);
//        alphaAnimation2.setDuration(3000);
//        alphaAnimation2.setFillAfter(true);
//        alphaAnimation2.setRepeatMode(Animation.REVERSE);
//        iv_fly.startAnimation(alphaAnimation2);
    }
}
